var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");
require("./database/connect.js"); //引入数据库连接文件
var cors = require("cors");
var { checkToken } = require("./utils/authToken");
const { createProxyMiddleware } = require("http-proxy-middleware");

var indexRouter = require("./routes/index");
var usersRouter = require("./routes/users");
var cartRouter = require("./routes/cart");

var app = express();

app.use(cors()); //解决跨域
app.use(   //服务端代理
  "/wzj",
  createProxyMiddleware({
    target: "https://www.wzj.com",
    changeOrigin: true,
    pathRewrite:{
      '^/wzj':''
    }
  })
);

app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));

app.use("/", indexRouter);
app.use("/users", usersRouter);
app.use(
  "/cart",
  (req, res, next) => {
    //对某一类接口，做出自定义中间件拦截
    // console.log(req.headers);
    checkToken(req, res, next); //触发token验证函数
  },
  cartRouter
);

module.exports = app;
